const {connectToIpcServer, sendReq, sendReqWithBins, binDatasKey}=require("../src_node/ipc_client");



const pipeName = "demo_pipe___";
const PIPE_PATH =  `\\\\.\\pipe\\${pipeName}`;
const L = console.log;
const E = console.error;


const beginSendReq=()=>{
    const buf1 = Buffer.allocUnsafe(4);
    buf1.writeUInt32BE(Math.floor(Math.random()*10000), 0);
    const buf2 = Buffer.from("这是一段文字"+new Date(), "utf-8");

    sendReqWithBins({
        Action:"file_dir_run",                // // Action:"cmd_run",
        Path: "d:/a/b/c",
    },[
        buf1,
        buf2,
    ]).then(r=>{
        L(`resp is`, JSON.stringify(r));
        if(r[binDatasKey]?.length>0){
            r[binDatasKey].forEach((b,i)=>{
                console.log(`resp binary items ${i} is`, b.toString("utf-8"));
            });
        }
    }).catch(e=>{
        E("err", e);
    });
    setTimeout(beginSendReq, 5000);
};


(()=>{
    const opt={
        reqCompress:     false,
        reqCompressLev: 0,
        respCompress:    false,
        hasErr:         (json)=>false,
        onPong:         ()=>{
            console.log("get pong at "+new Date());
        },
    };

    connectToIpcServer(PIPE_PATH, opt).then(()=>{
        beginSendReq();
    }).catch(e=>{
        console.error(e);
    });
})();
